﻿using System;
using System.IO;
using System.Windows.Forms;

namespace Bono.PhotoSorter
{
	public partial class ResolveConflict : Form
	{
		private readonly string _existingFile;
		private readonly string _newFile;

		public bool ApplyToAll { get; set; }
		public bool Abort { get; set; }
		public Constants.ResolveConflictAction ResolveAction { get; set; }

		public ResolveConflict(string existingFile, string newFile)
		{
			InitializeComponent();

			_existingFile = existingFile;
			_newFile = newFile;
		}

		private void ResolveConflict_Load(object sender, EventArgs e)
		{
			LoadUserSettings();
			ExistingFilePathLabel.Text = _existingFile;
			NewFilePathLabel.Text = _newFile;
			if (File.Exists(_existingFile))
			{
				ExistingPictureBox.ImageLocation = _existingFile;
				ExistingPictureBox.Load();
			}

			if (File.Exists(_newFile))
			{
				ConflictingPictureBox.ImageLocation = _newFile;
				ConflictingPictureBox.Load();
			}
		}

		private void LoadUserSettings()
		{
			if (Properties.Settings.Default.ResolveConflictOption == (int)Constants.ResolveConflictAction.KeepBoth)
			{
				KeepBothRadioButton.Checked = true;
			}
			else if (Properties.Settings.Default.ResolveConflictOption == (int)Constants.ResolveConflictAction.KeepExisting)
			{
				KeepExistingRadioButton.Checked = true;
			}
			else if (Properties.Settings.Default.ResolveConflictOption == (int)Constants.ResolveConflictAction.OverwriteExisting)
			{
				OverwriteExistingRadionButton.Checked = true;
			}

			ApplyToAll = Properties.Settings.Default.ApplyToAll;
			ApplyToAllCheckBox.Checked = ApplyToAll;
		}

		private void OKButton_Click(object sender, EventArgs e)
		{
			ApplyToAll = ApplyToAllCheckBox.Checked;
			
			if(OverwriteExistingRadionButton.Checked)
			{
				ResolveAction = Constants.ResolveConflictAction.OverwriteExisting;
			}
			else if(KeepExistingRadioButton.Checked)
			{
				ResolveAction = Constants.ResolveConflictAction.KeepExisting;
			}
			else
			{
				//This is the safest default action, keeps both files and creates a unique name for the conflicting file.
				ResolveAction = Constants.ResolveConflictAction.KeepBoth;
			}
			Properties.Settings.Default.ResolveConflictOption = (int) ResolveAction;
			Properties.Settings.Default.ApplyToAll = ApplyToAll;
			Properties.Settings.Default.Save();

			DialogResult = DialogResult.OK;
			Close();
		}

		private void AbortButton_Click(object sender, EventArgs e)
		{
			if(MessageBox.Show(this, 
				"This will terminate the entire process.\nFiles that have already processed will not be effected.\nContinue?",
				"Abort?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
			{
				Abort = true;
				DialogResult = DialogResult.Abort;
				Close();
			}
			else
			{
				Abort = false;
			}
			
		}

		private void PictureCompareSplitter_Resize(object sender, EventArgs e)
		{

			int panelSize = PictureCompareSplitter.Width / 2;
			PictureCompareSplitter.SplitterDistance = panelSize - PictureCompareSplitter.SplitterWidth;
			
		}

		private void PictureCompareSplitter_SplitterMoved(object sender, SplitterEventArgs e)
		{

		}

		private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
		{

		}

		private void ApplyToAllCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			ApplyToAll = ApplyToAllCheckBox.Checked;
		}
	}
}